package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.util.AppMetaData;
import com.xgkj.diyiketang.util.EatShowSetting;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class BaoMingZheAdapter extends BaseAdapter {

    private Context mContext;
    private String anchorId;
    private List<OrderInfo> mList = new ArrayList<OrderInfo>();
    private EatShowUserInfo uinfo;
    public BaoMingZheAdapter(Context context) {
        mContext = context;
    }

    public String getAnchorId() {
        return anchorId;
    }

    public void setAnchorId(String anchorId) {
        this.anchorId = anchorId;
    }

    public void updateData(List<OrderInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.baomingzhe_item, null);
            childHolder.headImg = (RoundImageView) convertView.findViewById(R.id.head_img);
            childHolder.bName = (TextView) convertView.findViewById(R.id.bmr_name);
            childHolder.bTime = (TextView) convertView.findViewById(R.id.wanchengshijian);
            childHolder.bOrderNo = (TextView) convertView.findViewById(R.id.bmr_order);
            childHolder.mhongbao = (ImageView) convertView.findViewById(R.id.hongbao);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final OrderInfo fansInfo = mList.get(position);
        if (fansInfo != null) {
            String headImgUrl = fansInfo.getUserLogo();
            if (headImgUrl != null && headImgUrl.length() > 0) {
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headImgUrl, new ViewUtil.Size(ViewUtil.dip2px(mContext, 40), ViewUtil.dip2px(mContext, 40)));

            } else {
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }
            if (fansInfo.getNickName() != null) {
                try {
                    childHolder.bName.setText(URLDecoder.decode(fansInfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (fansInfo.getCompleteDate() != null) {
                childHolder.bTime.setText(fansInfo.getCompleteDate());
            }
            String aid = getAnchorId();
           String localUid = EatShowApp.getInstance().getmUserInfo().getUserId();
                                        PalLog.e("创建人ID",aid);
                              PalLog.e("当前用户ID",localUid);
            if(aid!=null&&aid.equals(localUid)){
                if (fansInfo.getOrderNo() != null) {
                    childHolder.bOrderNo.setText("ta的饭票：" + fansInfo.getOrderNo());
                }
            }else {
                if (fansInfo.getOrderNo() != null) {
                    childHolder.bOrderNo.setText(null);
                }
            }

            int hb = fansInfo.getRedPacket();
            if(hb==1){
                childHolder.mhongbao.setBackgroundResource(R.drawable.hongbao1);
            }else if(hb==6){
                childHolder.mhongbao.setBackgroundResource(R.drawable.hongbao6);
            }
            else if(hb==30){
                childHolder.mhongbao.setBackgroundResource(R.drawable.hongbao30);
            }else if(hb==68){
                childHolder.mhongbao.setBackgroundResource(R.drawable.hongbao68);
            }else if(hb==198){
                childHolder.mhongbao.setBackgroundResource(R.drawable.hongbao198);
            }



        }

        return convertView;
    }

    class ChildHolder {
        RoundImageView headImg;
        TextView bName;
        TextView bTime;
        TextView bOrderNo;
        ImageView mhongbao;
    }
}
